<?
include "Courrier.php";


class ImpressionsController extends Controller {
    
	public $js = array("impressions");
	public $css = array("impressions");
	
	
	public function mainAction(){
		$connect = $this->connections;
		$courriers = $connect->fetchAllModels("Courrier",array("user_id"=>$_SESSION["user_id"]),array("date"=>"DESC"));
		foreach($courriers as $courrier){
			if($courrier->_client != ""){
				$courrier->_contact = $connect->fetchModels("Contacts",array("_id"=>$courrier->_client));
			}else{
				$courrier->_contact = $connect->fetchModels("Fournisseur",array("_id"=>$courrier->_fournisseur));
			}
		}
		if(isset($_GET["client"])){
			$contact = $connect->fetchModels("Contacts",array("_id"=>$_GET["client"]));
		}
		if(isset($_GET["fournisseur"])){
			$contact = $connect->fetchModels("Fournisseur",array("_id"=>$_GET["fournisseur"]));
		}		
		
		$contacts = array_merge($contacts,$fournisseurs);
		$vars = array(
			"courriers"=>$courriers,
			"contact"=>$contact
		);
		
		return $this->render("impressions/main",$vars);
	}
	public function saveCourrierAction(){
		
		$connect = $this->connections;
		
		$courrier = new Courrier();
		$courrier->fromArray($_POST);
		$courrier->date = date("Y-m-d");
		$courrier->user_id = $_SESSION["user_id"];
		$connect->save($courrier);
		
		return json_encode(array("response_code"=>"ok"));
	}
	public function downloadCourrierAction($courrier_id){
		$connect = $this->connections;
		
		$pdf = new PdfFile();
		$prefs = $connect->fetchModels("User_prefs",array("user_id "=>$_SESSION["user_id"]));
		$user = $connect->fetchModels("Users",array("_id"=>$_SESSION["user_id"]));
		$courrier = $connect->fetchModels("Courrier",array("_id"=>$courrier_id));
		if($courrier->_client != ""){
			$courrier->_contact = $connect->fetchModels("Contacts",array("_id"=>$courrier->_client));
		}else{
			$courrier->_contact = $connect->fetchModels("Fournisseur",array("_id"=>$courrier->_fournisseur));
		}
		return $this->render("downloads/courrier",array("courrier"=>$courrier,"prefs"=>$prefs,"pdf"=>$pdf,"user"=>$user));
	}
	public function removeCourrierAction($courrier_id){
		$connect = $this->connections;
		
		$connect->remove("Courrier",array("_id"=>$courrier_id));
		return json_encode(array("response_code"=>"ok"));
	}
	public function livreAchatsAction(){
		$connect = $this->connections;
		$dateDeb = date("Y-m-d",strtotime($_POST["date_deb"]));
		$dateFin = date("Y-m-d",strtotime($_POST["date_fin"]));
		$user_id = $_SESSION["user_id"];
		$mysql = $connect->getConnection();
		$query = "SELECT * FROM facture_fournisseur WHERE user_id = \"$user_id\" AND date BETWEEN \"$dateDeb\" AND \"$dateFin\" ORDER BY date ASC;";
		$res = $mysql->query($query);
		if($mysql->errno != 0){
			throw new Exception ("Erreur MySQL " . $mysql->error);
		}
		while($line = $res->fetch_object()){
			$line->fournisseur = $connect->fetchModels("Fournisseur",array("_id"=>$line->_fournisseur));
			$line->paiement = $connect->fetchModels("Paiement_fournisseur",array("_facture"=>$line->_id));
			$aFactures[] = $line;
		}
		$content = $this->render("downloads/livreAchats",array("factures"=>$aFactures));
		$content = utf8_decode($content);
		$html2pdf = new HTML2PDF('L','A4','fr', false, 'UTF-8');
		$html2pdf->writeHTML($content, false);
		$html2pdf->Output('livre_achats.pdf');
	}
	public function livreRecettesAction(){
		$connect = $this->connections;
		$dateDeb = date("Y-m-d",strtotime($_POST["date_deb"]));
		$dateFin = date("Y-m-d",strtotime($_POST["date_fin"]));
		$user_id = $_SESSION["user_id"];
		$mysql = $connect->getConnection();
		$query = "SELECT * FROM facture WHERE user_id = \"$user_id\" AND date BETWEEN \"$dateDeb\" AND \"$dateFin\" ORDER BY date ASC;";
		$res = $mysql->query($query);
		if($mysql->errno != 0){
			throw new Exception ("Erreur MySQL " . $mysql->error);
		}
		while($line = $res->fetch_object()){
			$line->client = $connect->fetchModels("Contacts",array("_id"=>$line->contact_id));
			$line->paiement = $connect->fetchModels("Paiement_client",array("_facture"=>$line->_id));
			$aFactures[] = $line;
		}
		$content = $this->render("downloads/livreRecettes",array("factures"=>$aFactures));
		$content = utf8_decode($content);
		$html2pdf = new HTML2PDF('L','A4','fr', false, 'UTF-8');
		$html2pdf->writeHTML($content, false);
		$html2pdf->Output('livre_recettes.pdf');
	}
	public function facturesAction(){
		$connect = $this->connections;
		$dateDeb = date("Y-m-d",strtotime($_POST["date_deb"]));
		$dateFin = date("Y-m-d",strtotime($_POST["date_fin"]));
		
		$pdf = new PdfFile();
		
		$user_id = $_SESSION["user_id"];
		
		$mysql = $connect->getConnection();
		$query = "SELECT facture.* FROM facture INNER JOIN paiement_client ON paiement_client._facture = facture._id "
				."WHERE user_id = \"$user_id\" AND facture.date BETWEEN \"$dateDeb\" AND \"$dateFin\" ORDER BY facture.date ASC;";
		$res = $mysql->query($query);
		if($mysql->errno != 0){
			throw new Exception ("Erreur MySQL " . $mysql->error);
		}
		while($facture = $res->fetch_object()){
			$facture->reglement = $connect->fetchModels("Type_reglement",array("_id"=>$facture->type_reglement));
			$facture->contact = $connect->fetchModels("Contacts",array("_id "=>$facture->contact_id));
			$lines = $connect->fetchAllModels("Line_facture",array("facture_id"=>$facture->_id));
			$facture->_lines = $lines; 
			$factures[] = $facture;
		}
		$user = $connect->fetchModels("Users",array("_id"=>$_SESSION["user_id"]));
		$prefs = $connect->fetchModels("User_prefs",array("user_id "=>$_SESSION["user_id"]));
		
		$vars = array(
			"factures"=>$factures,
			"pdf"=>$pdf,
			"user"=>$user,
			"prefs"=>$prefs
		);
		return $this->render("downloads/factures",$vars);
	}
}
?>